package com.yigang.controller;

import com.alibaba.fastjson.JSON;
import com.yigang.entity.ShopingCart;
import com.yigang.entity.Shoppingcart;
import com.yigang.entity.ShoppingcartVO;
import com.yigang.entity.User;
import com.yigang.service.ShopingCartService;
import com.yigang.service.UserService;
import com.yigang.utils.ResultUtil;
import com.yigang.utils.UserConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @PACKAGE_NAME: com.yigang.controller
 * @USER:
 * @DATE: 2020/12/18 14:11
 */
@Controller
@RequestMapping("cart")
public class CartController {
    @Autowired
    private ShopingCartService shopingCartService;

    /*
     * 添加到购物车
     */
    @RequestMapping("/addCart")
    @ResponseBody
    public ResultUtil addCart(Shoppingcart shopingCart, HttpServletRequest request, HttpSession session) {
        Object attribute = session.getAttribute(UserConst.LOGIN_USER_INFO);
        if (null != attribute) {
            User user = JSON.parseObject((String) attribute, User.class);
            Shoppingcart byId = shopingCartService.getByProductId(Integer.valueOf(shopingCart.getProductId()));
            if (byId != null) {
                byId.setProductQuantity(byId.getProductQuantity() + shopingCart.getProductQuantity());
                return ResultUtil.error("已经添加购物车");
            }
            shopingCart.setId(user.getId() + "");
            int i = shopingCartService.addCart(shopingCart);
             if (i > 0) {
                return ResultUtil.ok(1);
            } else {
                return ResultUtil.error("添加失败");
            }
        } else {
            return ResultUtil.error("请先登录");
        }
    }

    /*
     * 修改数量
     */
    @RequestMapping("/updateNumCart")
    @ResponseBody
    public ResultUtil updateNumCart(Shoppingcart shopingCart, HttpServletRequest request, HttpSession session) {
        int i = shopingCartService.updateNumCart(shopingCart);
         if (i > 0) {
            return ResultUtil.ok(1);
        } else {
            return ResultUtil.error("修改失败");
        }
    }

    /*
     * 删除商品
     */
    @RequestMapping("/delCart")
    @ResponseBody
    public ResultUtil delCart(Shoppingcart shopingCart, HttpServletRequest request, HttpSession session) {
        int i = shopingCartService.delCart(shopingCart);
         if (i > 0) {
            return ResultUtil.ok(1);
        } else {
            return ResultUtil.error("删除失败");
        }
    }

    /*
     * 清空
     */
    @RequestMapping("/delAllCart")
    @ResponseBody
    public ResultUtil delAllCart(Shoppingcart shopingCart, HttpServletRequest request, HttpSession session) {
        Object attribute = session.getAttribute(UserConst.LOGIN_USER_INFO);
        if (null != attribute) {
            User user = JSON.parseObject((String) attribute, User.class);
            if (user != null) {
                shopingCart.setId(user.getId() + "");
                int i = shopingCartService.delAllCart(shopingCart);
                if (i > 0) {
                    return ResultUtil.ok(1);
                } else {
                    return ResultUtil.error("清空失败");
                }
            }
        }
        return ResultUtil.error("清空失败");
    }

    /*
     * 商品列表
     */
    @RequestMapping("/getAllShoppingcart")
    public String getAllShoppingcart(Shoppingcart shopingCart, HttpServletRequest request, HttpSession session) {
        Object attribute = session.getAttribute(UserConst.LOGIN_USER_INFO);
        if (null != attribute) {
            User user = JSON.parseObject((String) attribute, User.class);
            if (user != null) {
                shopingCart.setId(user.getId() + "");
                List<ShoppingcartVO> list = shopingCartService.getAllShoppingcart(shopingCart);
                session.setAttribute("list", list);
                return "jsp/cart/showCart";
            }
            return "jsp/userLogin";
        }
        return "jsp/userLogin";
    }

}
